package com.example.order.controller;

import com.example.order.dto.OrderDTO;
import com.example.order.dto.OrderRequest;
import com.example.order.dto.OrderResponse;
import com.example.order.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

@RestController
@RequestMapping("/api/orders")
public class OrderController {
    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);
    private final OrderService orderService;
    private final Random random = new Random();

    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * 创建订单
     */
    @PostMapping
    public ResponseEntity<OrderResponse> createOrder(@RequestBody OrderRequest request) {
        try {
            OrderDTO order = orderService.createOrder(request);
            logger.info("订单创建成功: {}", order.getId());
            return ResponseEntity.ok(new OrderResponse(
                    order.getId(), 
                    "success", 
                    "订单创建成功"
            ));
        } catch (Exception e) {
            logger.error("创建订单失败", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(new OrderResponse(null, "error", e.getMessage()));
        }
    }

    /**
     * 支付订单
     */
    @PostMapping("/{orderId}/pay")
    public ResponseEntity<OrderResponse> payOrder(@PathVariable String orderId) {
        try {
            // 随机选择支付方式
            String[] paymentMethods = {"alipay", "wechat", "credit_card", "debit_card"};
            String paymentMethod = paymentMethods[random.nextInt(paymentMethods.length)];
            
            orderService.payOrder(orderId, paymentMethod);
            logger.info("订单支付成功: {}", orderId);
            return ResponseEntity.ok(new OrderResponse(
                    orderId, 
                    "success", 
                    "订单支付成功"
            ));
        } catch (Exception e) {
            logger.error("支付订单失败", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(new OrderResponse(orderId, "error", e.getMessage()));
        }
    }

    /**
     * 取消订单
     */
    @PostMapping("/{orderId}/cancel")
    public ResponseEntity<OrderResponse> cancelOrder(@PathVariable String orderId) {
        try {
            orderService.cancelOrder(orderId);
            logger.info("订单取消成功: {}", orderId);
            return ResponseEntity.ok(new OrderResponse(
                    orderId, 
                    "success", 
                    "订单取消成功"
            ));
        } catch (Exception e) {
            logger.error("取消订单失败", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(new OrderResponse(orderId, "error", e.getMessage()));
        }
    }
}
